/*把file1中的内容复制到file2中*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

//./12-fseek   file1
// arg[0] argv[1]
int main(int argc, char const *argv[])
{
    int fd;
    int ret = 0;
    if (argc != 2)
    {
        fprintf(stderr,"运行程序时，请代入参数 ./20-fdopen file\n");
        exit(-1);
    }
    fd = open(argv[1],O_RDWR|O_CREAT|O_TRUNC,0664);
    if (fd < 0)
    {
        perror("open");
        exit(-1);
    }
    int i = 0;
    char buf[20]={0};
    while (1)
    {
        i++;
        sprintf(buf,"i=%07d\n",i);
        printf("buf=%s\n",buf);
        ret = write(fd,buf,10);
        if (ret < 0)
        {
            perror("write");
            exit(-1);
        }
        usleep(1000*100);   //延迟100ms
    }
    

    return 0;
}
